import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class FileApps implements DataApps {
	
	private List<AudioPiece> musicList = new ArrayList();
	
	@Override
	public void readData() {
		
		try {
			BufferedReader br = new BufferedReader(new FileReader("Music.txt"));
			br.readLine(); // Ilk satırı okuyup geçiyor...
			String line;
			String[] lineArray;
			
			while((line = br.readLine()) != null) {
				
				lineArray = line.split(";");
				String[] lineInfo = new String[4];
				
				int i = 0;
				for(String s : lineArray) {
					lineInfo[i++] = s;
					
				}
							
				if(lineInfo[1].equals("CD")) {
					musicList.add(new CD(lineInfo[2], lineInfo[3]));
				}
				else if(lineInfo[1].equals("KASET")) {
					musicList.add(new Tape(lineInfo[2], lineInfo[3]));
				}
				else {
					System.err.println("Undefined Type at " + lineInfo[0] + ". index...");
				}
			}
			
			br.close();
			
			
		} catch (FileNotFoundException e) {
			System.err.println("Dosya henüz boş...");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void addData(String type, String artist, String title) {
		
		if (type.equals("CD")) {
			CD cd = new CD(artist, title);
			addCD(cd);
		}
		else if(type.equals("KASET")) {
			Tape tape = new Tape(artist, title);
			addTape(tape);
		}
		
	}

	@Override
	public void addData(AudioPiece ap) {
		if(ap instanceof CD) {
			addCD((CD)ap);
		}
		else if(ap instanceof Tape) {
			addTape((Tape)ap);
		}
	}
	
	private void addCD(CD cd) {
		if(musicList.contains(cd)) {
			System.out.println("Bu CD listede zaten var, eklenmedi...");
		}
		else {
			musicList.add(cd);
			System.out.println("CD listeye eklendi...");
			
			try {
				PrintWriter pw = new PrintWriter(new FileWriter("Music.txt", true));
				if(musicList.size() == 1) {
					pw.println("SIRANO;TYPE;ARTIST;TITLE");
				}	
				pw.println(musicList.lastIndexOf(cd) + ";CD;" + cd.getArtist() + ";" + cd.getTitle());
				pw.close();
				System.out.println("CD dosyaya yazıldı...");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void addTape(Tape tape) {
		if(musicList.contains(tape)) {
			int index = musicList.indexOf(tape);
			musicList.remove(index);
			musicList.add(index, tape);
			System.out.println("Bu kaset listede zaten var, yenisi ile değiştirildi...");
			PrintWriter pw;
			try {
				pw = new PrintWriter(new FileWriter("Music.txt"));
				pw.println("SIRANO;TYPE;ARTIST;TITLE");
				for(int i = 0; i < musicList.size(); i++) {
					if(musicList.get(i) instanceof Tape) {
						pw.println(i + ";KASET;" + musicList.get(i).getArtist() + ";" + musicList.get(i).getTitle());
					}
					else if(musicList.get(i) instanceof CD) {
						pw.println(i + ";CD;" + musicList.get(i).getArtist() + ";" + musicList.get(i).getTitle());
					}
				}
				System.out.println("Yeni kaset dosyaya yazıldı...");
				pw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		else {
			musicList.add(tape);
			System.out.println("Kaset listeye eklendi...");
			
			try {
				PrintWriter pw = new PrintWriter(new FileWriter("Music.txt", true));
				if(musicList.size() == 1) {
					pw.println("SIRANO;TYPE;ARTIST;TITLE");
				}
				pw.println(musicList.lastIndexOf(tape) + ";KASET;" + tape.getArtist() + ";" + tape.getTitle());
				pw.close();
				System.out.println("Kaset dosyaya yazıldı...");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
